package com.object.utils;


import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.Data;

import java.util.Map;

@Data
public class NettyResponseData
{
    public Integer code;
    public String msg;
    public Map<String, Object> data;

    public NettyResponseData msg(String msg)
    {
        this.msg = msg;
        return this;
    }

    public static NettyResponseData ok()
    {
        NettyResponseData responseData = new NettyResponseData();
        responseData.code = 200;
        responseData.msg = "成功";
        return responseData;
    }

    public NettyResponseData q(String key, Object value)
    {
        data.put(key, value);
        return this;
    }

    public static NettyResponseData err()
    {
        NettyResponseData responseData = new NettyResponseData();
        responseData.code = 201;
        responseData.msg = "错误";
        return responseData;
    }

    public String to()
    {
        return JSON.toJSONString(this);
    }


    //发送错误的信息
    public void sendErr(Channel channel)
    {
        channel.writeAndFlush(new TextWebSocketFrame(this.to()));
    }

    //发送正确的信息
    public void sendOk(Channel channel)
    {
        channel.writeAndFlush(new TextWebSocketFrame(this.to()));
    }

}
